package kr.hkit.summary_java.erp.UI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.myframework.db.DbUtil;
import kr.hkit.summary_java.erp.dao.CodeDAO;
import kr.hkit.summary_java.erp.dao.DepartmentDAO;
import kr.hkit.summary_java.erp.dao.EmployeeDAO;

public class Test extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JMenuBar menuBar;
	private ErpMenu mnEmp;
	private ErpMenu mnDept;
	private ErpMenu mnTitle;
	private String[] menuTitle;
	private Connection connection;
	private JPanel panel;
	private DepartmentDAO deptDao;
	private EmployeeDAO empDao;
	private CodeDAO titleDao;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					final Connection connection = ConnectionFactory
							.getConnection();
					Test frame = new Test(connection);
					frame.addWindowListener(new WindowAdapter() {
						public void windowClosing(WindowEvent e) {
							super.windowClosing(e);
							DbUtil.close(connection);
						}
					});
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Test(Connection connection) {
		this.connection = connection;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		

		menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		mnEmp = new ErpMenu("사원");
		menuBar.add(mnEmp);

		mnDept = new ErpMenu("부서");
		menuBar.add(mnDept);

		mnTitle = new ErpMenu("직책");
		menuBar.add(mnTitle);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		deptDao = new DepartmentDAO(connection);
		empDao = new EmployeeDAO(connection);
		titleDao = new CodeDAO(connection);
		
		addAction();
	}

	public void addAction() {
		int menuCnt = menuBar.getMenuCount();
		menuTitle = new String[menuCnt];

		for (int i = 0; i < menuCnt; i++) {
			menuTitle[i] = menuBar.getMenu(i).getText();
		}

		for (int j = 0; j < menuCnt; j++) {
			for (int i = 0; i < menuBar.getMenu(j).getItemCount(); i++) {
				menuBar.getMenu(j).getItem(i).addActionListener(this);
			}
		}
	}

	public void actionPerformed(ActionEvent e) {
		System.out.println(e.getActionCommand());
		String menu = e.getActionCommand();
		String sub = menu.substring(0, 2);
		String sub2 = menu.substring(3, 5);
		if (sub.equals("사원")) {
			panel = new PanelEmp(empDao);
			((PanelEmp) panel).setBtnOk(sub2);
		} else if (sub.equals("부서")) {
			panel = new PanelDept(deptDao);
			((PanelDept) panel).setBtnOk(sub2);
		} else if (sub.equals("직책")) {
			panel = new PanelTitle(titleDao);
			((PanelTitle) panel).setBtnOk(sub2);
		} else {
			System.out.println("err");
			System.exit(-1);
		}
		
		revalidPanel(panel);
	}

	private void revalidPanel(JPanel panel) {
		contentPane.removeAll();
		contentPane.add(panel);
		revalidate();
	}

}